package cn.com.fuhuitong.main.goods.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.goods.entity.GoodsDetailsResponse
import cn.com.fuhuitong.main.goods.entity.GoodsListResponse
import cn.com.fuhuitong.main.goods.entity.GoodsSpecsResponse
import cn.com.fuhuitong.main.goods.mode.GoodsManager
import cn.com.fuhuitong.main.host.entity.ConfigResponse
import io.reactivex.disposables.Disposable

class GoodsViewModel(application: Application) : BaseViewModel(application) {

    private val goodsManager: GoodsManager = GoodsManager()

    val goodsListDataLiveData = MutableLiveData<HttpResponse<GoodsListResponse>>()

    val goodsDetailsDataLiveData = MutableLiveData<HttpResponse<GoodsDetailsResponse>>()

    val goodsSpecsDataLiveData = MutableLiveData<HttpResponse<GoodsSpecsResponse>>()

    val goodsAddCartDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    val addGoodsCollectionDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()


    private var page: Int = 1
    private var limit: Int = 20


    /**
     * 获取商品列表数据
     */
    fun goodsList(categoryId: Int, search: String? = null) {
        val listener =
            object : ViewModelSubscribeListener<GoodsListResponse>(goodsListDataLiveData) {}
        val goodsListDisposable: Disposable =
            goodsManager.goodsList(
                page.toString(), limit.toString(),
                categoryId.toString(), search, listener
            )
        dispose.add(goodsListDisposable)
    }

    /**
     * 获取商品详情数据
     */
    fun goodsDetails(goodsId: Int) {
        val listener =
            object : ViewModelSubscribeListener<GoodsDetailsResponse>(goodsDetailsDataLiveData) {}
        val goodsDetailsDisposable: Disposable =
            goodsManager.goodsDetails(goodsId.toString(), listener)
        dispose.add(goodsDetailsDisposable)
    }

    /**
     * 获取商品规格数据
     */
    fun goodsSpecs(goodsId: Int) {
        val listener =
            object : ViewModelSubscribeListener<GoodsSpecsResponse>(goodsSpecsDataLiveData) {}
        val goodsSpecsDisposable: Disposable =
            goodsManager.goodsSpecs(goodsId.toString(), listener)
        dispose.add(goodsSpecsDisposable)
    }

    /**
     * 商品加入购物车
     */
    fun goodsAddCart(goodsId: String, num: String, specsId: String) {
        val listener =
            object : ViewModelSubscribeListener<BaseResponse>(goodsAddCartDataLiveData) {}
        val goodsSpecsDisposable: Disposable =
            goodsManager.goodsAddCart(
                BaseApplication.userToken(), goodsId, num, specsId,
                BaseApplication.userInfo()?.user_id.toString(), listener
            )
        dispose.add(goodsSpecsDisposable)
    }

    /**
     * 添加收藏
     */
    fun addGoodsCollection(goodsId: Int) {
        val listener =
            object : ViewModelSubscribeListener<BaseResponse>(addGoodsCollectionDataLiveData) {}
        val addGoodsCollectionDisposable: Disposable =
            goodsManager.addGoodsCollection(
                BaseApplication.userToken(), goodsId.toString(), listener
            )
        dispose.add(addGoodsCollectionDisposable)
    }



    val configDataLiveData = MutableLiveData<HttpResponse<ConfigResponse>>()
    /**
     * 获取配置信息
     * */
    fun configValue(name: Array<String>) {
        val listener =
            object : ViewModelSubscribeListener<ConfigResponse>(configDataLiveData) {}
        val configValueDisposable: Disposable = goodsManager.configValue(name,listener)
        dispose.add(configValueDisposable)
    }

    val configPhoneDataLiveData = MutableLiveData<HttpResponse<ConfigResponse>>()
    /**
     * 获取配置信息
     * */
    fun configValuePhone(name: Array<String>) {
        val listener =
            object : ViewModelSubscribeListener<ConfigResponse>(configPhoneDataLiveData) {}
        val configValueDisposable: Disposable = goodsManager.configValue(name,listener)
        dispose.add(configValueDisposable)
    }

}